let VS_move = 0.0
let VS_levelX = 0.0
let VS_perpendicularityY = 0.0
let VS_type = 0.0
let VS_min = -50
let VS_max = 50
let VS_status = 2


/**
 * 改变滑动条
 * @param type
 */
const VS_change = (type = 1) => {
    VS_status = type
    VS_clearCutting()
    if(type === 1){
        VS_loadTileset(new Cesium.Cartesian3(0.0, -1.0, 0.0))
    } else if(type === 2){
        VS_loadTileset(new Cesium.Cartesian3(-1.0, 0.0, 0.0))
    }else if(type === 3){
        VS_loadTileset(new Cesium.Cartesian3(0.0, 0.0, -1.0))
    }
}


/**
 * 开启立切
 * @param Cartesian3
 * @returns {Promise<unknown>}
 */
const VS_loadTileset = (Cartesian3 = new Cesium.Cartesian3(-1.0, 0.0, 0.0)) => {
    var clippingPlanes = new Cesium.ClippingPlaneCollection({
        planes : [
            // ClippingPlane对象数组集合
            new Cesium.ClippingPlane(Cartesian3,0.0), // 法线方向
        ],
        // edgeColor: Cesium.Color.RED,
        edgeWidth: 1.0, // 模型被裁切部分的截面线宽
        unionClippingRegions: true, //true 才能多个切割
    });

    // 定义一个ClippingPlaneCollection类，用来存储裁剪面
    var tileset = window.tileset
    tileset.clippingPlanes = clippingPlanes;
    return tileset.readyPromise.then(function() {
        var boundingSphere = tileset.boundingSphere;
        var radius = boundingSphere.radius;
        if (
            !Cesium.Matrix4.equals(
                tileset.root.transform,
                Cesium.Matrix4.IDENTITY
            )
        ) {
            const transformCenter = Cesium.Matrix4.getTranslation(
                tileset.root.transform,
                new Cesium.Cartesian3()
            );
            const transformCartographic = Cesium.Cartographic.fromCartesian(
                transformCenter
            );
            const boundingSphereCartographic = Cesium.Cartographic.fromCartesian(
                tileset.boundingSphere.center
            );
            const height =
                boundingSphereCartographic.height -
                transformCartographic.height;
            clippingPlanes.modelMatrix = Cesium.Matrix4.fromTranslation(
                new Cesium.Cartesian3(0.0, 0.0, height)
            );
        }
        for (var i = 0; i < clippingPlanes.length; ++i) {
            var plane = clippingPlanes.get(i);
            console.log(plane.normal, plane._normal,'normal')
            var planeEntity = viewer.entities.add({
                position : boundingSphere.center,
                id: 'ClipPlane01',
                plane : {
                    dimensions : new Cesium.Cartesian2(radius*2, radius*2),
                    material : Cesium.Color.WHITE.withAlpha(0.2),
                    plane : new Cesium.CallbackProperty(VS_createPlaneUpdateFunction(plane), false),
                    // outline : true,
                    // outlineColor : Cesium.Color.WHITE
                }
            });
            // planeEntity.plane.show = false
            console.log(planeEntity)
        }
        return tileset;
    }).catch(function(error) {
        console.log(error);
    });
}

/**
 * 退出立剖
 */
const VS_clearCutting = () =>{
    var tileset = window.tileset
    tileset.clippingPlanes = new Cesium.ClippingPlaneCollection();
    viewer.entities.removeById('ClipPlane01')
    VS_move = 0.0
    VS_levelX = 0.0
    VS_perpendicularityY = 0.0
}

// 计算移动位置
const VS_createPlaneUpdateFunction = (plane) => {
    return function() {
        let height = VS_move
        // if(data.single.levelX !== 0.0 || data.single.perpendicularityY !== 0.0){
        //     height = data.single.move * 5
        // }
        if(VS_status === 1){
            if(VS_type === 1){
                plane.distance = height
                return plane
            }else if(VS_type === 2){
                plane.distance = VS_levelX / 1000000 + height
                plane.normal._cartesian3 = new Cesium.Cartesian3(VS_levelX / 100,plane.normal.y,plane.normal.z)
                return plane
            }else if(VS_type === 3){
                plane.distance = VS_perpendicularityY / 1000000 + height
                plane.normal._cartesian3 = new Cesium.Cartesian3(plane.normal.x,plane.normal.y,VS_perpendicularityY / 100)
                return plane
            }
            else {
                return plane
            }
        }else if(VS_status === 2){
            if(VS_type === 1){
                plane.distance = height
                return plane
            }else if(VS_type === 2){
                plane.distance = VS_levelX / 1000000
                plane.normal._cartesian3 = new Cesium.Cartesian3(plane.normal.x,plane.normal.y,VS_levelX / 1000)
                return plane
            }else if(VS_type === 3){
                plane.distance = VS_perpendicularityY / 1000000
                plane.normal._cartesian3 = new Cesium.Cartesian3(plane.normal.x,VS_perpendicularityY / 1000,plane.normal.z)
                return plane
            }
            else {
                return plane
            }
        }else if(VS_status === 3){
            if(VS_type === 1){
                plane.distance = height
                return plane
            }else if(VS_type === 2){
                plane.distance = VS_levelX / 1000000 + height
                plane.normal._cartesian3 = new Cesium.Cartesian3(VS_levelX / 1000,plane.normal.y,plane.normal.z)
                return plane
            }else if(VS_type === 3){
                plane.distance = VS_perpendicularityY / 1000000 + height
                plane.normal._cartesian3 = new Cesium.Cartesian3(plane.normal.x,VS_perpendicularityY / 1000,plane.normal.z)
                return plane
            }
            else {
                return plane
            }
        }
    };
}
